import moment from 'moment';
const CLUMNS_OBJ = {
  contractName: {
    title: '合约名称',
    width: 110,
    hasE: true,
    isInput: false,
    style: 'left',
    tstyle: true,
  },
  decomposition: {
    title: '日期',
    width: 120,
    hasE: true,
    isInput: false,
    style: 'left',
    tstyle: true,
  },
  stationName: { title: '结算单元', width: 110, hasE: false, isInput: false, tstyle: true },
  version: { title: '版本', width: 60, hasE: false, isInput: false, tstyle: true },
  counterpartyName: { title: '合约对手方', width: 110, hasE: false, isInput: false, tstyle: true },
  income: { title: '收入(万元)', width: 80, hasE: false, isInput: false, tstyle: true },
  amount: { title: '金额(万元)', width: 80, hasE: false, isInput: false, tstyle: true },
  electrovalency: { title: '电价(元)', width: 80, hasE: false, isInput: false },
  quantity: { title: '电量(MWh)', width: 80, hasE: false, isInput: false, tstyle: true },
  regionName: { title: '省份', width: 100, hasE: true, isInput: false, tstyle: true },
  industryName: {
    title: '行业名称',
    width: 110,
    hasE: false,
    isInput: false,
    style: 'left',
    tstyle: true,
  },
  cycleName: { title: '周期模型', width: 100, hasE: false, isInput: false, tstyle: true },
  demandName: {
    title: '类型',
    width: 110,
    hasE: true,
    isInput: false,
    style: 'left',
    tstyle: true,
  },
  curveName: {
    title: '曲线',
    hasE: true,
    width: 110,
    isInput: false,
    style: 'left',
    tstyle: true,
  },
  curveContractName: {
    title: '合约',
    hasE: true,
    width: '10%',
    isInput: false,
    style: 'left',
    tstyle: true,
  },
  declarationName: {
    title: '类型',
    hasE: true,
    width: 180,
    isInput: false,
    style: 'left',
    tstyle: true,
  },
  declarationOperateName: {
    title: '操作',
    hasE: false,
    width: 130,
    isInput: false,
    style: 'left',
    tstyle: true,
  },
};
export const onCreatColumns = function (list, type, column, tableCol, precision) {
  if (list.length == 0) {
    return [];
  }

  let array = [];
  const obj = list[0];
  let number = 0;
  Object.keys(obj).forEach((keyVal) => {
    const item = CLUMNS_OBJ[keyVal];
    if (item !== undefined) {
      array.push({
        ...item,
        ellipsis: { showTitle: false },
        dataIndex: keyVal,
        key: keyVal,
        xNumber: number,
        fixed: 'left',
        render: (text, record, index) => {
          return tableCol(keyVal, record, index);
        },
      });
      number++;
    }
  });
  if (type === 'contract') {
    array.push({
      title: '电量偏差',
      ellipsis: { showTitle: false },
      dataIndex: 'offset',
      key: 'offset',
      xNumber: number,
      fixed: 'left',
      width: 80,
      render: (text, record, index) => {
        return tableCol('offset', record, index);
      },
    });
    number++;
  }
  if (type === 'decompose') {
    array.push({
      title: '电量偏差',
      ellipsis: { showTitle: false },
      dataIndex: 'offset',
      key: 'offset',
      xNumber: number,
      fixed: 'left',
      width: 80,
      render: (text, record, index) => {
        return tableCol('offset', record, index);
      },
    });
    number++;
  }

  obj.list &&
    obj.list.forEach((item) => {
      let title = '';
      if (obj.xType === 'timer') {
        title = moment(Number(item.xAxis)).format('HH:mm');
        if (title === '00:00') {
          title = '24:00';
        }
      } else {
        title = item.xAxis;
      }
      array.push({
        title: column(title, number, precision === 3 ? 100 : 120),
        ellipsis: { showTitle: false },
        width: precision === 3 ? 100 : 120,
        dataIndex: title,
        key: title,
        xNumber: number,
        render: (text, record, index) => {
          return tableCol(title, record, index);
        },
      });
      number++;
    });
  return array;
};

const getCLock = function (list, val) {
  return list.indexOf(val) >= 0;
};

export const onCreatData = function (list, type, precision) {
  let array = [];
  list.forEach((cell, index) => {
    var rowLock = cell.compileType === false;
    if (!rowLock) {
      rowLock = cell.rowAllLock;
    }
    var obj = {};
    let number = 0;
    let orginNum = 0;
    const rockCArray = cell.lockList || [];
    Object.keys(cell).forEach((keyVal) => {
      const item = CLUMNS_OBJ[keyVal];
      if (item !== undefined) {
        obj[keyVal] = {
          tstyle: item.tstyle,
          style: item.style,
          cLock: getCLock(rockCArray, orginNum),
          width: item.width,
          contractId: cell.contractId || undefined,
          counterpartyId: cell.counterpartyId || undefined,
          value: cell[keyVal],
          rowLock: rowLock,
          y: index,
          x: number,
          hasE: item.hasE,
          isInput: item.isInput,
          isCheck: false,
          isLock: false,
        };
        number++;
        orginNum++;
      }
    });
    if (type === 'contract') {
      let num = cell.list.reduce((pre, now) => now.value + pre, 0);
      let result = parseFloat(Number(num - cell.quantity).toFixed(precision));
      if (result === 0) {
        result = 0;
      }
      obj.offset = {
        width: 80,
        value: parseFloat(Number(result).toFixed(precision)),
        cLock: getCLock(rockCArray, orginNum),
        rowLock: rowLock,
        y: index,
        x: number,
        hasE: false,
        isInput: false,
        isCheck: false,
        isLock: false,
      }; //需要计算
      number++;
    }
    if (type === 'decompose') {
      let num = cell.list.reduce((pre, now) => now.value + pre, 0);
      let result = Number(num - cell.quantity).toFixed(precision);
      if (result === 0) {
        result = 0;
      }
      obj.offset = {
        width: 80,
        value: Number(result).toFixed(precision),
        cLock: getCLock(rockCArray, orginNum),
        rowLock: rowLock,
        y: index,
        x: number,
        hasE: false,
        isInput: false,
        isCheck: false,
        isLock: false,
      }; //需要计算
      number++;
    }
    cell.list &&
      cell.list.forEach((item) => {
        let title = '';
        if (cell.xType === 'timer') {
          title = moment(Number(item.xAxis)).format('HH:mm');
          if (title === '00:00') {
            title = '24:00';
          }
        } else {
          title = item.xAxis;
        }
        obj[title] = {
          xAxis: item.xAxis,
          width: 110,
          value: item.value != null ? Number(item.value).toFixed(precision) : '-',
          cLock: getCLock(rockCArray, orginNum),
          rowLock: rowLock,
          y: index,
          x: number,
          hasE: item.hasE === false ? false : true,
          isInput: item.isInput === false ? false : true,
          isCheck: false,
          isLock: false,
        };
        number++;
        orginNum++;
      });
    array.push(obj);
  });
  return array;
};

export const overMaxMessage = function (maxNum, timerArr, id) {
  let message = '';
  if (id === 1) {
    message = `您调整的${timerArr.join(
      ',',
    )}时间点的值大于最大上网电量${maxNum}，已重置为最大上网电量`;
  } else {
    message = `您调整的${timerArr.join(',')}时间点的值大于装机容量${maxNum}已重置为装机容量`;
  }
  return message;
};

export const precisionReg = function (precision) {
  if (precision === (6 || '6')) {
    return /^\D*(\d*(?:\.\d{0,6})?).*$/g;
  } else {
    return /^\D*(\d*(?:\.\d{0,3})?).*$/g; // '$1'
  }
};

//保留几位小数
export const point = function (value, precision) {
  let newValue = parseFloat(value);
  // let reg = /^[+-]?\d*\.?\d{0,3}$/;
  // let newNum = newValue;
  // if (!reg.test(newValue)) {
  // }
  let newNum = newValue.toFixed(precision);
  return newNum;
};
